﻿/*************************************************************************
* 计算机名：AA-FA-05
* CLR 版本：4.0.30319.42000
* 命名空间: Unity
* 类    名：IntptrHelper
* 当 前 域：HYRC
* 创 建 人：dailuyu
* 创建时间：2021/6/17 10:31:52
* 描    述：
*  V0.01 2021 初版
*
*  Copyright (c) 2021 aatsr Corporation. All rights reserved.
*  ┌──────────────────────────────────┐
*  │　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*  └──────────────────────────────────┘
***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Unity
{
    /// <summary>
    /// 指针操作帮组类
    /// </summary>
    public class IntptrHelper
    {
        /// <summary>
        /// 通过标示ST_VideoBaseInfo[]的intPtr获取List<VideoBaseInfo>
        /// </summary>
        /// <param name="intPtr"></param>
        /// <param name="dataLen"></param>        
        /// <returns></returns>
        public static List<T> GetListByIntPtr<T>(IntPtr intPtr, int dataLen)
        {
            List<T> list = new List<T>();
            if (dataLen >= 1)
            {
                T item = (T)Marshal.PtrToStructure(intPtr, typeof(T));
                list.Add(item);
                for (int i = 1; i < dataLen; i++)
                {
                    intPtr = IntPtr.Add(intPtr, Marshal.SizeOf(typeof(T)));
                    item = (T)Marshal.PtrToStructure(intPtr, typeof(T));
                    list.Add(item);
                }
            }
            return list;
        }

        public static T[] GetArrayByIntPtr<T>(IntPtr intPtr, int dataLen)
        {
            T[] list = new T[dataLen];
            if (dataLen >= 1)
            {
                T item = (T)Marshal.PtrToStructure(intPtr, typeof(T));
                list[0] = item;
                for (int i = 1; i < dataLen; i++)
                {
                    intPtr = IntPtr.Add(intPtr, Marshal.SizeOf(typeof(T)));
                    item = (T)Marshal.PtrToStructure(intPtr, typeof(T));
                    list[i] = item;
                }
            }
            return list;
        }
        /// <summary>
        /// 通过指针获取时间值    
        /// </summary>
        /// <param name="intPtr"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeByIntPtr(IntPtr intPtr)
        {
            SYSTEMTIME systemTime = (SYSTEMTIME)Marshal.PtrToStructure(intPtr, typeof(SYSTEMTIME));
            return new DateTime(systemTime.Year, systemTime.Month, systemTime.Day, systemTime.Hour, systemTime.Minute, systemTime.Second);
        }

        /// <summary>
        /// 创建数组句柄(调用完后 释放句柄)
        /// </summary>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="array">数组</param>
        /// <returns>数组地址</returns>
        public static IntPtr CreatArrayIntPtr<T>(Array array)
        {
            IntPtr p = IntPtr.Zero;
            if (array.Length > 0)
            {
                p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)) * array.Length);
                int i = 0;
                foreach (object obj in array)
                {
                    IntPtr pPonitor = new IntPtr(p.ToInt64() + Marshal.SizeOf(typeof(T)) * i);
                    Marshal.StructureToPtr(obj, pPonitor, false);
                    i++;
                }
            }
            return p;
        }
    }
}